package com.mcc.system.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mcc.common.utils.SecurityUtils;
import com.mcc.system.mapper.SysUserPostMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mcc.system.domain.SysPost;
import com.mcc.system.mapper.SysPostMapper;
import com.mcc.system.service.ISysPostService;

/**
 * 岗位信息 服务层处理
 *
 * @author gxy
 */
@Service
public class SysPostServiceImpl implements ISysPostService {

    @Autowired
    private SysPostMapper postMapper;

    @Autowired
    private SysUserPostMapper userPostMapper;

    /**
     * 查询岗位信息集合
     *
     * @param post 岗位信息
     * @return 岗位信息集合
     */
    @Override
    public List<SysPost> selectPostList(SysPost post) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(post)) {
            queryWrapper.like(ObjectUtil.isNotEmpty(post.getPostCode()), "post_code", post.getPostCode());
            queryWrapper.eq(ObjectUtil.isNotEmpty(post.getStatus()), "status", post.getStatus());
            queryWrapper.like(ObjectUtil.isNotEmpty(post.getPostName()), "post_name", post.getPostName());
            queryWrapper.orderByAsc("post_sort");
        }
        return postMapper.selectList(queryWrapper);
    }

    /**
     * 通过岗位ID查询岗位信息
     *
     * @param postId 岗位ID
     * @return 角色对象信息
     */
    @Override
    public SysPost selectPostById(Long postId) {
        return postMapper.selectById(postId);
    }

    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    @Override
    public List<Long> selectPostListByUserId(Long userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        queryWrapper.select("post_id");
        return userPostMapper.selectObjs(queryWrapper);
    }

    /**
     * 校验岗位名称是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostNameUnique(SysPost post) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("post_name", post.getPostName());
        queryWrapper.ne(ObjectUtil.isNotEmpty(post.getPostId()), "post_id", post.getPostId());
        return postMapper.selectCount(queryWrapper) == 0;
    }

    /**
     * 校验岗位编码是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostCodeUnique(SysPost post) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("post_code", post.getPostCode());
        queryWrapper.ne(ObjectUtil.isNotEmpty(post.getPostId()), "post_id", post.getPostId());
        return postMapper.selectCount(queryWrapper) == 0;
    }

    /**
     * 新增保存岗位信息
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public int insertPost(SysPost post) {
        post.setCreateBy(SecurityUtils.getUsername());
        post.setCreateTime(new Date());
        return postMapper.insert(post);
    }

    /**
     * 修改保存岗位信息
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public int updatePost(SysPost post) {
        post.setUpdateBy(SecurityUtils.getUsername());
        post.setUpdateTime(new Date());
        return postMapper.updateById(post);
    }

    /**
     * 删除岗位信息
     *
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public int deletePostById(Long postId) {
        return postMapper.deleteById(postId);
    }

    /**
     * 批量删除岗位信息
     *
     * @param postIds 需要删除的岗位ID
     * @return 结果
     */
    @Override
    public int deletePostByIds(Long[] postIds) {
        if (postIds.length == 0) return 0;
        return postMapper.deleteBatchIds(ListUtil.toList(postIds));
    }

    @Override
    public List<SysPost> selectPostsByUserId(Long userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.inSql("post_id", "select post_id from sys_user_post where user_id=" + userId);
        return postMapper.selectList(queryWrapper);
    }

}
